package com.dftc.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.dftc.pojo.Person;
import com.dftc.service.IUserService;
 
/**
 * 基于Restful风格架构测试
 * 
 * GET（SELECT）：从服务器取出资源（一项或多项）。
POST（CREATE）：在服务器新建一个资源。
PUT（UPDATE）：在服务器更新资源（客户端提供改变后的完整资源）。
PATCH（UPDATE）：在服务器更新资源（客户端提供改变的属性）。
DELETE（DELETE）：从服务器删除资源。

http://www.ruanyifeng.com/blog/2014/05/restful_api.html

GET /zoos：列出所有动物园
POST /zoos：新建一个动物园
GET /zoos/ID：获取某个指定动物园的信息
PUT /zoos/ID：更新某个指定动物园的信息（提供该动物园的全部信息）
PATCH /zoos/ID：更新某个指定动物园的信息（提供该动物园的部分信息）
DELETE /zoos/ID：删除某个动物园
GET /zoos/ID/animals：列出某个指定动物园的所有动物
DELETE /zoos/ID/animals/ID：删除某个指定动物园的指定动物

 * 
 */
@Controller
public class DekotaAction  implements BaseController {
 
    /** 日志实例 */
    private static final Logger logger = Logger.getLogger(DekotaAction.class);
    
    @Resource
	private IUserService userService;

    @RequestMapping(value = "/hello", produces = "text/plain;charset=UTF-8")
    public @ResponseBody
    String hello() {
        return "你好！hello";
    }
 
    @RequestMapping(value = "/say/{msg}", produces = "application/json;charset=UTF-8")
    public @ResponseBody
    String say(@PathVariable(value = "msg") String msg) {
        return "{\"msg\":\"you say:'" + msg + "'\"}";
    }
 
    @RequestMapping(value = "/person/{id:\\d+}", method = RequestMethod.GET)
    public @ResponseBody
    Object getPerson(@PathVariable("id") int id) {
		Person user = this.userService.getUserById(id);
		if(user == null){
		}
		return new JSONObject();
    }
 
    @RequestMapping(value = "/person/{id:\\d+}", method = RequestMethod.DELETE)
    public @ResponseBody
    Object deletePerson(@PathVariable("id") int id) {
        logger.info("删除人员信息id=" + id);
		int  userId = this.userService.deleteUser(id);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msg", RESULT_DELETE_SUCCESS);
        jsonObject.put("userId", userId);
        return jsonObject;
    }
 
    @RequestMapping(value = "/person", method = RequestMethod.POST)
    public @ResponseBody
    Object addPerson(Person person) {
        logger.info("注册人员信息成功id=" + person);
        this.userService.addUser(person);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msg", RESULT_ADD_SUCCESS);
        return jsonObject;
    }
 
    @RequestMapping(value = "/person", method = RequestMethod.PUT)
    public @ResponseBody
    Object updatePerson(Person person) {
        userService.updateUser(person);
        logger.info("更新人员信息id=" + person.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msg", RESULT_EDIT_SUCCESS);
        return jsonObject;
    }
 
    /*
    @RequestMapping(value = "/person", method = RequestMethod.PATCH)
    public @ResponseBody
    List<Person> listPerson(@RequestParam(value = "name", required = false, defaultValue = "") String name) {
        logger.info("查询人员name like " + name);
        List<Person> lstPersons = new ArrayList<Person>();
        Person person = new Person();
        person.setUserName("张三");
        person.setPassword("this");
        person.setAge(25);
        person.setId(101);
        lstPersons.add(person);
 
        Person person2 = new Person();
        person2.setUserName("李四");
        person2.setPassword("oop");
        person2.setAge(23);
        person2.setId(102);
        lstPersons.add(person2);
 
        Person person3 = new Person();
        person3.setUserName("王五");
        person3.setPassword("oo");
        person3.setAge(27);
        person3.setId(103);
        lstPersons.add(person3);
 
        System.out.println(lstPersons);
        return lstPersons;
    }
 */

    @RequestMapping(value = "/person", method = RequestMethod.PATCH)
    public @ResponseBody
    List<Person> listPerson(@RequestParam(value = "name", required = false, defaultValue = "") String name) {
        logger.info("查询人员name like " + name);
        List<Person> lstPersons = new ArrayList<Person>();
        Person person = new Person();
        person.setUserName("张三");
        person.setPassword("this");
        person.setAge(25);
        person.setId(101);
        lstPersons.add(person);
 
        Person person2 = new Person();
        person2.setUserName("李四");
        person2.setPassword("oop");
        person2.setAge(23);
        person2.setId(102);
        lstPersons.add(person2);
 
        Person person3 = new Person();
        person3.setUserName("王五");
        person3.setPassword("oo");
        person3.setAge(27);
        person3.setId(103);
        lstPersons.add(person3);
 
        System.out.println(lstPersons);
        return lstPersons;
    }
    
}
